 <?php
include 'base.php';
include 'require_seller.php';
include 'connectdb.php';

function get_date($code){
    $sql = "SELECT date from bill where invoice_code = '".$code."' limit 1";
    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result))        
        return $row['date'];
}

function get_customer($code){
    $sql = "SELECT customer_code from bill where invoice_code = '".$code."' limit 1";
    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result))
        return $row['customer_code'];
}

if ($_POST){
    $sql="UPDATE bill SET status='0' where code ='".$_POST['code']."'";
    if (mysql_query($sql, $co))
            echo "Đã cập nhật thành công!";    
    else{                  
            header("HTTP/1.0 404 Not Found");
            header('HTTP', true, 500);
        }    
}
?>

<script>
	 $(document).ready(function () {
        $('#delete, #error').hide(); 
	 	$("#select_code").change(function(){
	 		if ($(this).val() != -1)
	 			window.location.href = 'edit_hoa_don.php?code=' + $(this).val();
	 	});
        $(".class-delete").each(function(){
            $(this).click(function(){
                if (!confirm('Bạn có chắc chắn muốn xóa')) return false;                                                
                var code = $(this).closest('tr').attr("id");                
                var parent = $(this).closest('tr');                     
                $.ajax({
                type: "post",
                url: "",
                data: "code=" + code + "&type=delete",
                success: function () {                                        
                    $("#delete").show();
                    $('#error').hide();
                    $("#total_invoice").val($("#total_invoice").val() - parseInt ($(parent).children('.total').text()));
                    $(parent).hide();                    
                },
                error: function() {                       
                    $('#error').show();                      
                    $("#delete").hide();
                }                
            }); 
            });
        });
                
	 });
</script>

<?php
$seller_code = $_COOKIE['name'];
$code = -1;
if (isset($_GET['code'])){
	$code = $_GET['code'];
}
$sql = "SELECT invoice_code from bill where seller_code ='".$seller_code."' group by invoice_code";
$res = mysql_query($sql);
echo "<select id=\"select_code\"><option value=\"-1\" selected=\"selected\">Chọn mã</option>";
	while($row = mysql_fetch_array($res))
		if ($row['invoice_code']!=$code)
        {
        ?>          
            <option value="<?php echo"".$row['invoice_code']."";?>"><?php echo"".$row['invoice_code']."";?></option>
        <?php
        }
        else {
         	?>          
            <option selected="seleted" value="<?php echo"".$row['invoice_code']."";?>"><?php echo"".$row['invoice_code']."";?></option>
        <?php
         } 
        echo "</select>";          
if ($code >= 0){
    ?>
    <div id ="info">                    
        <span>
            Mã Hóa đơn
            <input id = "invoice_code" disabled = "disabled" value="<?php echo $code;?>">        
            Mã khách hàng
            <input id = "customer_code" disabled = "disabled" value="<?php echo get_customer($code);?>">
        </span>
        <span>
            Mã nhân viên<input id = "seller_code" value="<?php echo $_COOKIE['name'];?>" disabled="disabled" />
        </span>
        <span>
            Ngày giao dịch <input type="text" name="date" id = "date" disabled="disabled" value="<?php echo get_date($code); ?>">
        </span>
        
    </div>
    <table id="list_table" class="table" style="table-layout:fixed;" >
        <tr>
            <th>Mã </th>
            <th>Mã Sản Phẩm</th>
            <th>Số lượng </th>
            <th>Khuyến mại</th>
            <th>Giá tiền</th>
            <th>Tổng tiền</th>
            <th></th>
        </tr>
        <tbody>
    <?php
    $sql = "SELECT * from bill where invoice_code ='".$code."' and status = '1'";    
    $sum= 0;
    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result)){
        $sum += $row['total'];
        echo "<tr id ='".$row['code']."'>";
        echo "<td> <a href=\"hoa_don_chi_tiet.php?code=" . $row['code']."\">".$row['code']."</a></td>";
        echo "<td>".$row['product_code']."</td>";        
        echo "<td>".$row['quantity']."</td>";
        echo "<td>".$row['discount']."</td>";
        echo "<td>".$row['price']."</td>";
        echo "<td  class='total'>".$row['total']."</td>";
        echo "<td><button class='btn class-delete'>Xóa</button></td>";
        echo "</tr>";
    }
    ?>
        </tbody>
        <tr>
            <td colspan="4"><div id = "error">Có lỗi xảy ra</div>
            <div id = "delete">Đã xóa thành công</div></td>
            <td>Tổng hóa đơn</td>
            <td><input id="total_invoice" value="<?php echo $sum;?>" disabled="disabled"></td>
        </tr>

    </table>
    <?php
}
?>


<?php include 'footer.php';?>